package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Permission;
import com.yl.auth.entity.Role;
import com.yl.auth.entity.User;
import com.yl.auth.mapper.RoleMapper;
import com.yl.auth.model.entity.Quanxian;
import com.yl.auth.service.PermissionService;
import com.yl.auth.service.QuanxianService;
import com.yl.auth.service.RoleService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ln
 * @since 2023-07-18
 */
@Api(tags = "运营端-角色管理")
@RestController
@RequestMapping("/admin/auth/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private PermissionService permissionService;
    @Resource
    private RoleMapper roleMapper;

    @ApiOperation(value = "角色全查")
    @GetMapping("/findRole/{page}/{limit}")
    public Result<IPage> findRole(@PathVariable Integer page,@PathVariable Integer limit){
        Page<Role> page1 = new Page<>(page,limit);
        IPage<Role> roleList = roleService.findRole(page1);
        return Result.ok(roleList);
    }
    //删除===========================================
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/removeid/{id}")
    public Result remove(@PathVariable Integer id){
        roleService.updaterole(id);
        return Result.ok();
    }
    @ApiOperation(value = "根据id查询角色")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Integer id){
        Role role=roleService.getByrole(id);
        return Result.ok(role);
    }
    //添加===========================================
    @ApiOperation(value = "查询所有的权限-添加展示")
    @GetMapping("/findquanxian")
    public Result findquanxian(){
        List<Permission> list = permissionService.listquanxian();
        return Result.ok(list);
    }
    @ApiOperation(value = "添加角色及分配权限")
    @PostMapping("/addrole")
    public Result addrole(@RequestParam(value ="quanxianList") List<Integer> quanxianList,@RequestBody Role role){
        role.setType(1);
        roleService.save(role);
        Integer rid = roleMapper.selectMaxId();
        System.err.println(rid);
        for (Integer id : quanxianList) {
            roleService.saverole(id,rid);
        }
        return Result.ok();
    }
    //修改============================================
    @ApiOperation(value = "根据id查询用户信息-修改回显")
    @GetMapping("/getByIdrole/{id}")
    public Result getByIdrole(@PathVariable Integer id){
        Role byId = roleService.getById(id);
        return Result.ok(byId);
    }
    @ApiOperation(value = "根据id查询中间表角色信息-修改回显")
    @GetMapping("/getrole/{id}")
    public Result getrole(@PathVariable Integer id){
        List<Integer> list=roleService.getrole(id);
        System.out.println(list+"---------------------------");
        return Result.ok(list);
    }
    @ApiOperation(value = "根据id修改")
    @PostMapping("/update")
    public Result update(@RequestBody Role role,@RequestParam(value ="quanxianList") List<Integer> quanxianList){
        roleService.updateById(role);
        roleService.removeRole(role.getId());
        for (Integer id1 : quanxianList) {
            roleService.saverole(id1,role.getId());
        }
        return Result.ok();
    }
}

